package com.campus_information_portal.controller;

import com.campus_information_portal.entity.PartyMember;
import com.campus_information_portal.entity.PartyMemberPartyStudent;
import com.campus_information_portal.entity.PartyMemberTemp;
import com.campus_information_portal.service.PartyMemberService;
import com.campus_information_portal.service.PartyService;
import com.campus_information_portal.service.StudentService;
import com.campus_information_portal.util.Message;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 20155883 万珊
 * @description 党员控制层
 * @project_name PartyMemberController
 * @package com.campus_information_portal.controller
 * @create 2018-05-27 20:22
 **/
@Controller
@RequestMapping("/partyMember")
@ResponseBody
@CrossOrigin(origins = "*")
public class PartyMemberController {
    @Autowired
    private PartyMemberService partyMemberService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private PartyService partyService;

    /**
     * @param partyMemberPartyStudent
     * @Description 查询党支部成员信息(分支部、身份、年级)
     * @author 20155883 万珊
     * @methodName queryPartyMemberByConditions
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @RequestMapping(value = "/queryPartyMemberByConditions",method = RequestMethod.GET)
    public Message queryPartyMemberByConditions( PartyMemberPartyStudent partyMemberPartyStudent){
        System.out.println(partyMemberPartyStudent.toString());
        return partyMemberService.queryPartyMemberByConditions(partyMemberPartyStudent);
    }
    /**
     * @param partyMemberAccount
     * @Description 根据党支部账户（教师编号/学生编号）查询所在党组织编号
     * @author 20155883 万珊
     * @methodName queryPON
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @RequestMapping(value = "/queryPON",method = RequestMethod.GET)
    public Message queryPartyMemberByConditions(@Param("partyMemberAccount")int partyMemberAccount){
        int partyOrganizationNum=partyMemberService.findPONBypartyMemberAccount(partyMemberAccount);
        System.out.println("partyOrganizationNum="+partyOrganizationNum);
        Message message=new Message();

        return message.success().add("PON",partyOrganizationNum);
    }

    /**
     * @Description 查询年级,党支部
     * @author 20155883 万珊
     * @methodName queryConditions
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @RequestMapping(value = "/queryConditions",method = RequestMethod.GET)
    public Message queryConditions(){
        Message message=new Message();
        List<String> partyName=partyService.queryPartyName();
        List<String> grade=studentService.queryGrade();
        message.add("partyName",partyName);
        message.add("grade",grade);
        return message;
    }


    /**
     * @param partyMemberAccount
     * @Description 根据党支部账户（教师编号/学生编号）查询所在党员信息
     * @author 20155883 万珊
     * @methodName queryPartyMemberByPMA
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @RequestMapping(value = "/queryPartyMemberByPMA",method = RequestMethod.GET)
    public Message queryPartyMemberByPMA(@Param("partyMemberAccount")int partyMemberAccount){
        return partyMemberService.queryPartyMemberByPMA(partyMemberAccount);
    }

//    /**
//     * @param partyMember
//     * @Description 修改党支部成员信息
//     * @author 20155883 万珊
//     * @methodName updatePartyMember
//     * @returnType Message
//     * @create 2018/4/26 8:45
//     **/
//    @RequestMapping(value = "/updatePartyMember",method = RequestMethod.POST)
//    public Message updatePartyMember(@RequestBody PartyMember partyMember){
//        //System.out.println("-----------");
//        //System.out.println("partyMember.account="+partyMember.getPartyMemberAccount()+"--partyMember.type="+partyMember.getPartyMemberType());
//        Message message=new Message();
//        boolean flag1=partyMemberService.updatePartyMember(partyMember);
//        boolean flag2=partyMemberService.updateStudentPoli(partyMember);
//        //System.out.println("flag1="+flag1+"--flag2="+flag2);
//        if ((flag1==true)&&(flag2==true)){
//            message.success();
//        }else {
//            message.fail().add("1102","修改失败");
//        }
//        return message;
//    }

    /**
     * @param partyMemberAccount
     * @Description 删除党支部成员信息
     * @author 20155883 万珊
     * @methodName deletePartyMember
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @RequestMapping(value = "/deletePartyMember",method = RequestMethod.GET)
    public Message deletePartyMember(@Param("partyMemberAccount")int partyMemberAccount){
        Message message=new Message();
        boolean flag1=partyMemberService.deletePartyMember(partyMemberAccount);
        boolean flag2=partyMemberService.updateStudentPoliWithO(partyMemberAccount);
        if ((flag1==true)&&(flag2==true)){
            message.success();
        }else {
            message.fail().add("1106","删除失败");
        }
        return message;
    }

     /**
     * @param partyMemberTemp
     * @Description 保存
     * @author 20155883 万珊
     * @methodName savePartyMember
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @RequestMapping(value = "/savePartyMember",method = RequestMethod.POST)
    public Message savePartyMember(@RequestBody PartyMemberTemp partyMemberTemp){
        System.out.println("---"+partyMemberTemp.getPartyMemberAccount()+partyMemberTemp.getTypeNow()+partyMemberTemp.getTypeLast());
        return partyMemberService.savePartyMember(partyMemberTemp);
    }

    /**
     * @param partyMember
     * @Description 提交
     * @author 20155883 万珊
     * @methodName commitPartyMember
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @RequestMapping(value = "/commitPartyMember",method = RequestMethod.POST)
    public Message commitPartyMember(@RequestBody PartyMember partyMember){
        return partyMemberService.commitPartyMember(partyMember);
    }


    /**
     * @param partyMember
     * @Description 回滚
     * @author 20155883 万珊
     * @methodName rollback
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @RequestMapping(value = "/rollback",method = RequestMethod.POST)
    public Message rollback(@RequestBody PartyMember partyMember){
        return partyMemberService.rollback(partyMember);
    }





    /**
     * @param partyMemberAccount
     * @Description 查询该学生政治类型
     * @author 20155883 万珊
     * @methodName queryStudentPoli
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @RequestMapping(value = "/queryStudentPoli",method = RequestMethod.GET)
    public Message queryStudentPoli(@Param("partyMemberAccount")int partyMemberAccount){
        return partyMemberService.queryStudentPoli(partyMemberAccount);
    }
}
